package com.example.beautyhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.beautyhub.model.*;
import com.example.beautyhub.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/order")
@Api(tags = "订单")
public class OrderController {

    @Resource
    private OrderService orderService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private BeautyProductService beautyProductService;
    @Autowired
    private EmployeeController employeeController;

    @ApiOperation("查询列表")
    @GetMapping("/list")
    public List<OrderInfo> getOrderInfoList() {
        List<OrderInfo> orderLists = new ArrayList<>();
        List<Orders> ordersList = orderService.list();
        for (Orders orders : ordersList) {
            OrderInfo OrderInfo = new OrderInfo();
            OrderInfo.setOrderId(orders.getId());
            QueryWrapper<Customers> customersQueryWrapper = new QueryWrapper<>();
            customersQueryWrapper.eq("id",orders.getCustomerId());
            List<Customers> customersList = customerService.list(customersQueryWrapper);
            OrderInfo.setCustomersList(customersList);
            QueryWrapper<Employees> employeesQueryWrapper = new QueryWrapper<>();
            employeesQueryWrapper.eq("id",orders.getEmployeeId());
            List<Employees> employeesList = employeeService.list(employeesQueryWrapper);
            OrderInfo.setEmployeesList(employeesList);
            QueryWrapper<Products> productsQueryWrapper = new QueryWrapper<>();
            productsQueryWrapper.eq("id",orders.getProductId());
            List<Products> productsList = beautyProductService.list(productsQueryWrapper);
            OrderInfo.setProductsList(productsList);
            OrderInfo.setSize(orders.getSize());
            orderLists.add(OrderInfo);
        }
        return orderLists;//返回分页数据
    }

    public List<Orders> getOrdersList(QueryWrapper wrapper){
        return orderService.list(wrapper);
    }

    @ApiOperation("销售统计")
    @GetMapping("/BySize")
    public HashMap<String, Integer> findBySize() {
        HashMap<String, Integer> hashMap = new HashMap<>();
        for (Orders orders : orderService.list()) {
            QueryWrapper<Orders> wrapper = new QueryWrapper<>();
            wrapper.eq("employee_id",orders.getEmployeeId());
            List<Orders> ordersList = orderService.list(wrapper);
            Integer sum = 0;
            for (Orders order : ordersList) {
                sum += order.getSize();
            }
            Employees employees = employeeController.findById(orders.getEmployeeId());
            String name = employees.getName();
            hashMap.put(name, sum);
        }
        return hashMap;
    }

    @ApiOperation("盈利统计")
    @GetMapping("/BySizeYin")
    public HashMap<String, Double> findByYin() {
        HashMap<String, Double> hashMap = new HashMap<>();
        double one = 0;
        double two = 0;
        double three = 0;
        double four = 0;
        for (Orders orders : orderService.list()) {
            int month = orders.getCreatedTime().getMonth() + 1;
            if (month <= 3){
                one += orders.getPrice();
                hashMap.put("1季度",one);
            }else if (month > 3 && month <= 6){
                two += orders.getPrice();
                hashMap.put("2季度",two);
            }else if (month >6 && month <= 9){
                three += orders.getPrice();
                hashMap.put("3季度",three);
            }else {
                four += orders.getPrice();
                hashMap.put("4季度",four);
            }
        }
        return hashMap;
    }

    @ApiOperation("销售统计，产品展示")
    @GetMapping("/orderList")
    public List<Products> getOrderList(){
        List<Products> productsArrayList = new ArrayList<>();
        for (OrderInfo orderInfo : getOrderInfoList()) {
            List<Products> productsList = orderInfo.getProductsList();
            for (Products products : productsList) {
                Products product = new Products();
                product.setId(products.getId());
                product.setName(products.getName());
                product.setDescription(products.getDescription());
                product.setAmount(products.getAmount());
                product.setSize(orderInfo.getSize());
                product.setStockOutTime(products.getStockOutTime());
                productsArrayList.add(product);
            }
        }
        return productsArrayList;
    }

    @GetMapping("/{id}")
    public Orders findById(@PathVariable Integer id) {
        return orderService.getById(id);
    }

    @PostMapping("/")
    public void add(@RequestBody Orders order) {
        order.setCreatedTime(new Date());
        orderService.save(order);
    }

    @PutMapping("/{id}")
    public void update(@PathVariable Integer id, @RequestBody Orders order) {
        order.setId(id);
        orderService.updateById(order);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        orderService.removeById(id);
    }
}

